import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;

public class login implements ActionListener {
	JButton ok = new JButton("OK");
	final JTextField user_tf = new JTextField();
	final JPasswordField pass_tf = new JPasswordField();
	JDialog frame;
	private String usr = "";
	private String psw = "";

	public login() {
		frame = new JDialog();
		frame.setModal(true);
		JPanel panel = new JPanel();
		JLabel login_label = new JLabel("Login");

		// pannello
		panel.setSize(new Dimension(300, 200));
		panel.setMaximumSize(new Dimension(300, 200));
		panel.setMinimumSize(new Dimension(300, 200));
		panel.setPreferredSize(new Dimension(300, 200));
		panel.setLayout(null);

		// JLabel
		JLabel user = new JLabel("Username");
		JLabel password = new JLabel("Password");

		// login label
		login_label.setBounds(36, 20, 150, 20);
		login_label.setFont(new Font("Impact", Font.TRUETYPE_FONT, 20));

		// ok button
		ok.setBounds(260, 170, 20, 18);
		ok.setBorder(null);
		ok.addActionListener(this);
		ok.setFont(new Font("Impact", Font.TRUETYPE_FONT, 16));

		// user,password label
		user.setBounds(36, 70, 100, 20);
		password.setBounds(36, 112, 100, 20);

		// campi di inserimento
		user_tf.setBounds(120, 70, 140, 20);
		pass_tf.setBounds(120, 112, 140, 20);

		// aggiungo al panello principale
		panel.add(login_label);
		panel.add(user);
		panel.add(password);
		panel.add(user_tf);
		panel.add(pass_tf);
		panel.add(ok);
		frame.add(panel);
		frame.setResizable(false);
		frame.pack();
		frame.setVisible(true);

		frame.addWindowListener(new WindowListener() {
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	public void setParam() {
		usr = user_tf.getText();
		// Creo la stringa dall'array
		psw = new String(pass_tf.getPassword());
	}

	public String[] getData() {
		String[] info = { usr, psw };
		return info;
	}

	public static void main(String[] Args) {//http://www.ketek.it/
		String[] info = new login().getData();
		System.out.println("Username - " + info[0] + "\n");
		System.out.println("Password - " + info[1] + "\n");
		System.exit(0);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		setParam();
		frame.dispose();
	}
}